﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Service;

namespace community
{
    public partial class Two_switch : Form
    {
       
        residentcheck re = new residentcheck();
        string cr_JMBH;
        public Two_switch()
        {
            InitializeComponent();
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void label16_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            re.ShowDialog();
            cr_JMBH = residentcheck.resident_JMBH.Trim();
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string sql = "select inhabitantcode_chr as 居民编号,residentname_chr as 居民姓名,identitycard_chr as 身份证号,sex_chr as 性别,birth_dat as 出生日期, communityname_chr as 所属社区,communitycode_chr as 社区编号,familycode_chr as 家庭编号,company_chr as 工作单位,culture_vchr as 文化程度,phone_chr as 联系电话,addressprovince_vchr as 家庭地址_省,addresscity_vchr as 家庭地址_市,addresscounty_vchr as 家庭地址_县,patientcardid_chr as 诊疗卡号,careerstyle_chr as 职业 from  [community].[dbo].[T_CS_INHABITANTRECORD_NEW] where inhabitantcode_chr='" + cr_JMBH + "'";
            string sql1 = "select CBYX_VCHR as 初步印象,ZCYY_VCHR as 转出原因,ZYJWS_VCHR as 主要既往病历,ZLJG_VCHR as 治疗经过,CREATEDATE_DAT as 创建时间,DOCTORIDMANE_VCHR as 转诊医生,LINKPHONENUMER_VCHR as 联系电话,ORGANNAME2_VCHR as 转出机构 from T_CS_TRANSFERDIAGNOSIS_OUT where inhabitantcode_chr='" + cr_JMBH + "'";
            Cls_DBHelper help = new Cls_DBHelper();
            SqlDataReader dr = help.m_clsGetReader(sql);
            Cls_DBHelper help1 = new Cls_DBHelper();
            SqlDataReader dr1 = help1.m_clsGetReader(sql1);
            if (dr.Read())
            {
                string str1 = dr["家庭地址_省"].ToString();
                string str2 = dr["家庭地址_市"].ToString();
                string str3 = dr["家庭地址_县"].ToString();
                textBox24.Text = dr["居民姓名"].ToString();
                textBox23.Text = dr["性别"].ToString();
                textBox22.Text = dr["工作单位"].ToString();
                textBox21.Text = dr["居民编号"].ToString();
                textBox20.Text = dr["出生日期"].ToString();
                textBox19.Text = dr["联系电话"].ToString();
                textBox18.Text = dr["诊疗卡号"].ToString();
                textBox17.Text = dr["职业"].ToString();
                textBox16.Text = str1 + str2 + str3;
                textBox15.Text = dr["身份证号"].ToString();
                textBox14.Text = dr["文化程度"].ToString();
                textBox13.Text = dr["联系电话"].ToString();
                label15.Text = dr.GetValue(1).ToString();
                label16.Text = dr.GetValue(3).ToString();
            }
            dr.Close();

            if (dr1.Read())
            {
                richTextBox1.Text = dr1["初步印象"].ToString();
                richTextBox2.Text = dr1["转出原因"].ToString();
                richTextBox3.Text = dr1["主要既往病历"].ToString();
                richTextBox4.Text = dr1["治疗经过"].ToString();
                //dateTimePicker1.Text = dr1["接诊日期"].ToString();
                dateTimePicker1.Text = dr1.GetValue(4).ToString();
                textBox12.Text = dr1["转诊医生"].ToString();
                comboBox1.Text = dr1["转出机构"].ToString();
            }
            dr1.Close();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Two_switch_Load(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            textBox12.Text = "";
            textBox13.Text = "";
            comboBox1.Text = "";
            comboBox2.Text = "";
            label15.Text = "";
            label17.Text = "";
            richTextBox1.Text = "";
            richTextBox2.Text = "";
            richTextBox3.Text = "";
            richTextBox4.Text = "";
            dateTimePicker3.Text = "";
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();

            string sql4 = "select count(inhabitantcode_chr) from T_CS_TRANSFERDIAGNOSIS_OUT where inhabitantcode_chr='" + cr_JMBH + "'";
            SqlCommand df4 = new SqlCommand(sql4, cn);
            SqlDataReader dr = df4.ExecuteReader();
            dr.Read();
            string d = dr.GetValue(0).ToString();
            dr.Close();
            if (d == "0")
            {
                string sql = "insert into T_CS_TRANSFERDIAGNOSIS_OUT(INHABITANTCODE_CHR,CBYX_VCHR,ZCYY_VCHR,ZYJWS_VCHR,ZLJG_VCHR,CREATEDATE_DAT,DOCTORIDMANE_VCHR,LINKPHONENUMER_VCHR,ORGANNAME2_VCHR) Values(" + textBox21.Text + ",'" + richTextBox1.Text + "','" + richTextBox2.Text + "','" + richTextBox3.Text + "','" + richTextBox4.Text + "'," + dateTimePicker2.Text + ",'" + textBox12.Text + "','" + textBox13.Text + "','" + comboBox1.Text + "')";
                SqlCommand df = new SqlCommand(sql, cn);

                try
                {
                    df.ExecuteNonQuery();
                    MessageBox.Show("插入成功", "提示");
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("不能插入数据！" + "\n" + ex.ToString(), "提示！", MessageBoxButtons.OKCancel);
                }

            }
            else
            {
                string sql = "update T_CS_TRANSFERDIAGNOSIS_OUT set CBYX_VCHR='" + richTextBox1.Text + "',ZYXBS_VCHR='" + richTextBox2.Text + "',ZYJWS_VCHR='" + richTextBox3.Text + "',ZLJG_VCHR='" + richTextBox4.Text + "',DOCTORIDMANE_VCHR='" + textBox12.Text + "',ORGANNAME2_VCHR='" + comboBox1.Text + "',CREATEDATE_DAT=" + dateTimePicker2.Text;
                SqlCommand dc = new SqlCommand(sql, cn);

                try
                {
                    dc.ExecuteNonQuery();
                    MessageBox.Show("更新成功", "提示");
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("无法更新数据！" + "\n" + ex.ToString(), "提示！", MessageBoxButtons.OKCancel);
                }
            }
            cn.Close();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(SQL.conStr);
            cn.Open();

            string sql = "delete T_CS_TRANSFERDIAGNOSIS_OUT where inhabitantcode_chr=" + textBox21.Text;
            SqlCommand dc = new SqlCommand(sql, cn);
            try
            {
                dc.ExecuteNonQuery();
                MessageBox.Show("删除成功", "提示");
                richTextBox1.Text = "";
                richTextBox2.Text = "";
                richTextBox3.Text = "";
                richTextBox4.Text = "";
                comboBox1.Text = "";
                comboBox2.Text = "";
                textBox12.Text = "";
                textBox12.Text = "";
                textBox13.Text = "";

            }
            catch (SqlException ex)
            {
                MessageBox.Show("删除失败！" + "\n" + ex.ToString(), "提示！", MessageBoxButtons.OKCancel);
            }
        }
    }
}
